﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using LibMS.Models;
using Dapper;

namespace LibMS.Filters
{
    public class LibMSRoleProvider : RoleProvider
    {
        public string ErrorMessage { get; set; }

        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        public override string[] GetRolesForUser(string username)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@UserName", username);
            var userRole = _unitOfWork.LibRole.QuerySpNT(out message, StringConstant.TABLE_LIBROLE + StringConstant.SP_LIBROLE_GETUSERROLE, parameters);
            if (userRole == null)
            {
                ErrorMessage = message;
                return null;
            }
            return userRole.Select(v => v.RoleName).ToArray<string>();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@UserName", username);
            parameters.Add("@RoleName", roleName);
            var isUserInRole = _unitOfWork.CmsUser.QuerySpNT(out message, StringConstant.TABLE_CMSUSER + StringConstant.SP_CMSUSER_USERINROLE, parameters);
            if (isUserInRole == null)
            {
                if (!string.IsNullOrEmpty(message))
                {
                    ErrorMessage = string.Empty;
                }
                else
                {
                    ErrorMessage = message;
                }
                return false;
            }
            else
            {
                if (isUserInRole.Count() <= 0)
                {
                    ErrorMessage = string.Empty;
                    return false;
                }
                else
                {
                    ErrorMessage = string.Empty;
                    return true;
                }
            }
        }

        #region Not Implement Method

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName { get; set; }

        #endregion
    }
}